package com.bytz.interceptors;

import cn.hutool.json.JSONUtil;
import com.bytz.common.exception.BytzBootException;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;


public class PostInterceptorFilter {

    @SuppressWarnings("unchecked")
    public void filterFields(String body, List<String> matchFields) {

        if (StringUtils.isBlank(body)) {

            return;
        }

        Map<String, Object> fieldMap = JSONUtil.toBean(body, Map.class);

        matchFields.forEach(field -> {

            Object object = fieldMap.get(field);

            if (object != null) {

                throw new BytzBootException(String.format("Property '%s' at offset '%s' is invalid", field, object));
            }
        });
    }
}
